// pages/index/edit/edit.js
import {
  http
} from '../../../utils/requestUtil'
Page({
  data: {
    title: "",
    content: "",
    date: "2025-06-12",
    noteId: null // 用于保存便签ID
  },
  // 标题输入变化
  onTitleChange(e) {
    this.setData({
      title: e.detail.value
    });
  },

  // 内容输入变化
  onContentChange(e) {
    this.setData({
      content: e.detail.value
    });
  },

  // 保存便签
  async saveNote() {
    const { title, content, date } = this.data;
    
    if (!title.trim()) {
      wx.showToast({
        title: '标题不能为空',
        icon: 'none'
      });
      return;
    }
    
    // 这里调用保存接口
    const res =await http.put("/memo/schedule",{
      id:this.data.id,
      title:this.data.title,
      content:this.data.content,
      scheduleDate:this.data.date
    })
    
    wx.showToast({
      title: '保存成功',
      icon: 'success',
      duration: 1500,
      success: () => {
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      }
    });
  },

  // 删除便签
  async deleteNote() {
    wx.showModal({
      title: '删除确认',
      content: '确定要删除这个便签吗？',
      confirmColor: '#e74c3c',
      success: async(res) => {
        if (res.confirm) {
          const r = await http.delete("/memo/schedule/"+this.data.id)  
          wx.showToast({
            title: '已删除',
            icon: 'success',
            success: () => {
              setTimeout(() => {
                wx.navigateBack();
              }, 1000);
            }
          });
        }
      }
    });
  },

  // 返回上一页
  goBack() {
    wx.navigateBack();
  },
  async onLoad(options){
    const date = options.date
    this.setData({date:date})
    const res = await http.get("/memo/schedule/date/"+date)
    this.setData({title:res.data.title,date:date,content:res.data.content,id:res.data.id})
    console.log(date)
  }
});